import { ObjType } from '@/@type'
import { obj2Param } from '@/utils'
import { getObjectUrl, isCosUrl } from './cos/cosUpload'
import config from '@/config'
import Vue from 'vue'
import { router } from '@/router'
// import { setGlobalDialog } from './auth'
console.log(router, '====router==')
//路由跳转 解决堆栈问题
/**
 * 
  navigateTo, redirectTo 只能打开非 tabBar 页面。
  switchTab 只能打开 tabBar 页面。
  reLaunch 可以打开任意页面。
 */
export function navigateTo(
  value: string | number,
  query?: ObjType,
  type?: 'navigateTo' | 'reLaunch' | 'redirectTo' | 'switchTab',
  isH5?: false
) {
  console.log('navigateTo', value, type)
  // setGlobalDialog(false)
  const platform = config.VUE_APP_PLATFORM
  if (isH5 && platform === 'h5') {
    router.push({
      path: value,
      query
    })
  }
  if (typeof value === 'number') {
    return uni.navigateBack({ delta: Math.abs(value) })
  }
  const hasQuery = query && Object.keys(query).length
  const queryStr = hasQuery ? '?' + obj2Param(query) : ''
  const path = value + queryStr
  console.log(getCurrentPage(), '==getCurrentPage()', value)
  if (value === '/' + getCurrentPage().cuttentRoute) {
    return
  }
  type ? uni[type]({ url: path }) : uni.navigateTo({ url: path })
}

export const previewImage = async (index: number, urls: string[]) => {
  const currentUrl = [] as string[]
  for (let i = 0; i < urls.length; i++) {
    const url = urls[i]
    if (isCosUrl(url)) {
      const res = await getObjectUrl(url)
      currentUrl.push(res)
    } else {
      currentUrl.push(url)
    }
  }
  uni.previewImage({
    current: index,
    urls: currentUrl
  })
}
/**
 * 拨打电话
 * @param phone 号码
 */
export const callTelephone = (phone) => {
  uni.makePhoneCall({
    phoneNumber: phone + '',
    success: (res) => {
      console.log('callTelephone success', res)
    },
    fail: (err) => {
      console.log('callTelephone fail', err)
    }
  })
}
/**
 * 记录页面栈
 * @returns
 */
export const getCurrentPage = () => {
  const pages = getCurrentPages()
  const page = pages[pages.length - 1]
  let fromPath = ''
  const fullPath = page?.$page?.fullPath
  if (pages.length > 1) {
    const fromPage = pages[pages.length - 2]
    fromPath = fromPage?.route
  } else if (pages.length === 1) {
    const path = pages[0]
    fromPath = path?.route
  }
  return {
    cuttentRoute: page?.route,
    options: page?.options,
    fromPath,
    fullPath
  }
}
/**
 * 判断是h5下是否是微信环境
 * @returns
 */
export function isWxBrowswer() {
  const ua = navigator.userAgent.toLowerCase()
  return ua.match(/MicroMessenger/i)
}
/**
 * 小程序回退阻止提示
 * @param message 回退阻止时候的提示语
 */
export const enable = (message) => {
  //  #ifdef MP-WEIXIN
  wx.enableAlertBeforeUnload({
    message: message
  })
  // #endif

  //  #ifdef MP-ALIPAY
  my.enableAlertBeforeUnload({
    message: message
  })
  // #endif

  //  #ifdef MP-TOUTIAO
  tt.enableAlertBeforeUnload({
    message: message
  })
  // #endif
}

export const disable = () => {
  //  #ifdef MP-WEIXIN
  wx.disableAlertBeforeUnload({})
  // #endif

  //  #ifdef MP-ALIPAY
  my.disableAlertBeforeUnload({})
  // #endif

  //  #ifdef MP-TOUTIAO
  tt.disableAlertBeforeUnload({})
  // #endif
}

/**
 * 统一nexttick
 * @param cb 执行语句
 * @param evn 需要用定时器控制的环境
 * @param time 定时器时间
 * @param ctx 执行上下文
 */
export const nextTick = (cb, evn: any[] = [], time = 300, ctx: any) => {
  const platform = config.VUE_APP_PLATFORM
  if (
    (evn.includes('tt') && platform === 'mp-toutiao') ||
    (evn.includes('my') && platform === 'mp-alipay')
  ) {
    setTimeout(() => {
      cb.call(ctx)
    }, time)
  } else {
    //  #ifdef MP-ALIPAY
    setTimeout(() => {
      cb.call(ctx)
    }, time)
    // #endif
    //  #ifndef MP-ALIPAY
    Vue.nextTick(() => {
      cb.call(ctx)
    })
    // #endif
  }
}
